package com.linkoog.devtools.http.toolwindow.workbenche.left.http_service.toolbar;

import com.intellij.ui.components.JBCheckBox;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;

import javax.swing.*;
import java.awt.*;
import java.util.ArrayList;
import java.util.List;

/**
 */
public class ModuleFilterPopup extends JPopupMenu {

    private int height;

    private List<String> moduleNameList;

    private List<JBCheckBox> checkBoxList;

    private JPanel contextPane;

    private JPanel operatePane;

    @Nullable
    private ChangeCallback changeCallback;
    @Nullable
    private ChangeAllCallback changeAllCallback;


    public ModuleFilterPopup(List<String> moduleNameList){
        this.moduleNameList = new ArrayList<>(moduleNameList);
        this.checkBoxList = new ArrayList<>();

        initComponent();
    }



    private void initComponent() {
        this.setLayout(new BorderLayout());

        int rows;
        if (moduleNameList.size() %2  == 0){
            rows = (moduleNameList.size() /2);
        }else {
            rows = 1 + (moduleNameList.size() /2);
        }

        height = rows * 17 + 50;
        this.setPopupSize(new Dimension(310, height));
        this.setAutoscrolls(true);

        contextPane = new JPanel();
        contextPane.setMinimumSize(new Dimension(330, rows * 17));
        contextPane.setLayout(new GridLayout( rows, 2, 3, 3));
        for (String moduleName : moduleNameList) {
            JBCheckBox checkBox = new JBCheckBox(handlerName(moduleName), true);
            checkBox.setToolTipText(moduleName);
            checkBox.addActionListener(e -> {
                if (changeCallback != null) {
                    changeCallback.changed(checkBox, moduleName);
                }
            });
            checkBoxList.add(checkBox);
            contextPane.add(checkBox);
        }

        JButton selectAll = new JButton("Select All");
        selectAll.addActionListener(e -> {
            if (getSelectedValues().size() < checkBoxList.size()) {
                List<String> changes = new ArrayList<>();
                //检查其他的是否被选中乳沟没有就选中他们
                for (int i = 0; i < checkBoxList.size(); i++) {
                    JCheckBox checkBox = checkBoxList.get(i);
                    if (!checkBox.isSelected()) {
                        checkBox.setSelected(true);
                        changes.add(checkBox.getToolTipText());
                    }
                }
                if (!changes.isEmpty() && changeAllCallback != null) {
                    changeAllCallback.changed(changes, true);
                }
            }
        });


        operatePane = new JPanel();
        operatePane.add(selectAll);

        JButton close = new JButton("Close");
        close.addActionListener(e -> this.setVisible(false));
        operatePane.add(close);

        this.add(contextPane, BorderLayout.CENTER);
        this.add(operatePane, BorderLayout.SOUTH);
    }


    private String handlerName(String name){
        if (StringUtils.isBlank(name)){
           return name;
        }

        if (name.length() > 20){
            return name.substring(0, 20) + "...";
        }
        return name;
    }

    public List<String> getSelectedValues() {
        List<String> selectedValues = new ArrayList<>();
        for (int i = 0; i < checkBoxList.size(); i++) {
            JCheckBox checkBox = checkBoxList.get(i);
            if (checkBox.isSelected()) {
                selectedValues.add(checkBox.getToolTipText());
            }
        }

        return selectedValues;
    }

    public List<String> getModuleNameList() {
        return moduleNameList;
    }

    public int getHeight(){
        return height;
    }

    @Override
    public void setVisible(boolean b) {
        super.setVisible(b);
    }

    public void setChangeCallback(@Nullable ChangeCallback changeCallback) {
        this.changeCallback = changeCallback;
    }

    public void setChangeAllCallback(@Nullable ChangeAllCallback changeAllCallback) {
        this.changeAllCallback = changeAllCallback;
    }

    @Nullable
    public ChangeCallback getChangeCallback() {
        return changeCallback;
    }

    @Nullable
    public ChangeAllCallback getChangeAllCallback() {
        return changeAllCallback;
    }

    public interface ChangeCallback {
        void changed(@NotNull JCheckBox checkBox, @NotNull String moduleName);
    }

    public interface ChangeAllCallback {
        void changed(@NotNull List<String> moduleNames, boolean selected);
    }
}
